<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/12/31
 * Time: 17:21
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\lib;

class Str
{
    /**
     * 生成唯一订单号
     * @return string
     */
    public static function Random(): string
    {
        return date('YmdHis') . substr(microtime(), 2, 5) . mt_rand(10000, 99999);
    }

    /**
     * @return string
     */
    public static function RandomStr(): string
    {
        return date('YmdHis') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    /**
     * 生成唯一随机数
     * @param int $num
     * @return string
     */
    public static function RandStr($num = 8): string
    {
        $strand = time() . (double)microtime() * 1000000;
        if (strlen($strand) < $num) {
            $strand = md5(str_pad($strand, $num, "0", STR_PAD_LEFT) . time());
        }
        if ($num > 30) {
            $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $str_rand = '';
            for ($i = $num; $i > 0; $i--) {
                $str_rand .= $char[mt_rand(0, strlen($char) - 1)];
            }
            $string = $strand . $str_rand;
        } else {
            $string = $strand;
        }
        return mb_substr($string, 0, $num);
    }

    public static function guid()
    {
        if (function_exists('com_create_guid')) {
            return com_create_guid();
        } else {
            mt_srand((int)microtime());
            $charid = strtoupper(md5(uniqid((string)rand(), true)));
            $hyphen = chr(45);
            $uuid = substr($charid, 0, 8) . $hyphen
                . substr($charid, 8, 4) . $hyphen
                . substr($charid, 12, 4) . $hyphen
                . substr($charid, 16, 4) . $hyphen
                . substr($charid, 20, 8);
            return $uuid;
        }
    }

    /**
     * 分享图片生成
     * @param $gData  商品数据，array
     * @param $codeName 二维码图片
     * @param $fileName string 保存文件名,默认空则直接输入图片
     */

   public static function createSharePng($userid,$gData,$codeName,$fileName = ''){
        //创建画布
        $im = imagecreatetruecolor(400, 600);
        //填充画布背景色
        $color = imagecolorallocate($im, 255, 255, 255);
        imagefill($im, 0, 0, $color);
        //字体文件
        $font_file =public_path('static'). "/msyh.ttf";

        //设定字体的颜色
        $font_color_1 = ImageColorAllocate ($im, 140, 140, 140);
        $font_color_2 = ImageColorAllocate ($im, 28, 28, 28);
        $font_color_3 = ImageColorAllocate ($im, 27, 26, 26);
        $fang_bg_color = ImageColorAllocate ($im, 254, 216, 217);


        //头像
        $yuanpic =yuan_img($gData['pic'],'jpeg','user_'.$userid.'.png');

        $goodImg = createImageFromFile($yuanpic);
        imagecopyresized($im, $goodImg, 50, 60, 0, 0, 80, 80, 132, 132);

        //昵称
        $theTitle = cn_row_substr($gData['title'],2,19);
        imagettftext($im, 14,0, 150,100, $font_color_3 ,$font_file, $gData['title']);

        //二维码
        list($code_w,$code_h) = getimagesize($codeName);
        $codeImg = createImageFromFile($codeName);
        imagecopyresized($im, $codeImg, 50, 210, 0, 0, 290, 290, $code_w, $code_h);

        //温馨提示
        imagettftext($im, 13,0, 110, 530, $font_color_1 ,$font_file, '扫码进入收获健康财富');


        //输出图片
        if($fileName){
            imagepng ($im,$fileName);
        }else{
            Header("Content-Type: image/png");
            imagepng ($im);
        }
        //释放空间
        imagedestroy($im);
        imagedestroy($goodImg);
        imagedestroy($codeImg);
    }

    /**
     * 计算配送相隔距离
     */
    public static function getShippingDistances($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
    {
        $EARTH_RADIUS = 6370.996; //地球半径系数
        $PI = 3.1415926535898;

        $radLat1 = $lat1 * $PI / 180.0;
        $radLat2 = $lat2 * $PI / 180.0;

        $radLng1 = $lng1 * $PI / 180.0;
        $radLng2 = $lng2 * $PI / 180.0;

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $distance = $distance * $EARTH_RADIUS * 1000;

        if ($unit == 2) {
            $distance /= 1000;
            $gongli= round($distance, $decimal);
        }else{
            $gongli=round($distance, $decimal);
        }

        return $gongli;
    }
}